package util.arff;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;


public class WrapperCaller {

    public final static String CONFIG_1 = "features_manualglob21.conf";
    public final static String CONFIG_2 = "features_manual.conf";
    private final static String separator = System.getProperty("file.separator");
    
    public static void callWrapper(String name, String outputPath, String inputPath, String configPath, String pp2featuresPath, boolean isSVM1) throws IOException, InterruptedException {
		File outputFile = new File(outputPath);
		outputFile.mkdirs();
		
		// for debugging
		String batchCommand = createBatchCommand(name, outputPath, inputPath, configPath,pp2featuresPath, isSVM1);
		System.out.println("Batchcommand: "+batchCommand);
		
		Process process1 = Runtime.getRuntime().exec(createBatchCommand(name, outputPath, inputPath, configPath,pp2featuresPath, isSVM1));
		process1.waitFor();
		StringWriter writer1 = new StringWriter();
		IOUtils.copy(process1.getErrorStream(), writer1, "UTF-8");
		StringWriter writer2 = new StringWriter();
		IOUtils.copy(process1.getInputStream(), writer2, "UTF-8");
		System.out.println(writer1.toString());
		System.out.println(writer2.toString());
		String csvPath = outputFile.getAbsolutePath() + separator + name + ".csv";
		File csvFile = new File(csvPath);
		csvFile.delete();
		try{
			File dir = new File(inputPath);
			FileUtils.deleteDirectory(dir);
		}catch(Exception ex) {
	    	System.err.println("Fehler beim loeschen der out-verzeichnisse: "+ex.getMessage());
	    	ex.printStackTrace();
		}
    }
    
    public static void callWrapperNoDel(String name, String outputPath, String inputPath, String configPath, String pp2featuresPath, boolean isSVM1) throws IOException, InterruptedException {
		File outputFile = new File(outputPath);
		outputFile.mkdirs();
		
		// for debugging
		String batchCommand = createBatchCommand(name, outputPath, inputPath, configPath,pp2featuresPath, isSVM1);
		System.out.println("Batchcommand: "+batchCommand);
		
		Process process1 = Runtime.getRuntime().exec(createBatchCommand(name, outputPath, inputPath, configPath,pp2featuresPath, isSVM1));
		process1.waitFor();
		StringWriter writer1 = new StringWriter();
		IOUtils.copy(process1.getErrorStream(), writer1, "UTF-8");
		StringWriter writer2 = new StringWriter();
		IOUtils.copy(process1.getInputStream(), writer2, "UTF-8");
		System.out.println(writer1.toString());
		System.out.println(writer2.toString());
		String csvPath = outputFile.getAbsolutePath() + separator + name + ".csv";
		File csvFile = new File(csvPath);
		csvFile.delete();
//		try{
//			File dir = new File(inputPath);
//			FileUtils.deleteDirectory(dir);
//		}catch(Exception ex) {
//	    	System.err.println("Fehler beim loeschen der out-verzeichnisse: "+ex.getMessage());
//	    	ex.printStackTrace();
//		}
    }

    public static String createBatchCommand(String name, String output, String input, String configPath, String pp2featuresPath, boolean isSVM1) {
	String config = (isSVM1) ? CONFIG_1 : CONFIG_2;
	String result = pp2featuresPath + " -p " +
			input +
			" -a prot.arff -f " +
			configPath + separator + 
			config +
			" --csv-file=" +
			output + separator + name +
			".csv --arff-file=" +
			output + separator + name +
			".arff";
	return result;
    }
    
	public static void mergeWrapperOutput(String pathFirstArff, String pathSecondArff, String outputPath) throws IOException {
	    File firstArff = new File(pathFirstArff);
	    File secondArff = new File(pathSecondArff);
	    if (!firstArff.exists() || !secondArff.exists()) {
		throw new FileNotFoundException();
	    }
	    BufferedReader firstFileReader = new BufferedReader(new FileReader(firstArff));
	    File outputFile = new File(outputPath);
	    File dir = new File(outputFile.getParent());
	    dir.mkdirs();
	    if (!outputFile.exists()) {
		outputFile.createNewFile();
	    }
	    BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));
	    String nextLine = "";
	    boolean isData = false;
	    while ((nextLine = firstFileReader.readLine()) != null) {
		if (nextLine.contains("@DATA")) {
		    isData = true;
		}
		if (!isData || (isData && nextLine != "")) {
		    writer.append(nextLine);
		    writer.newLine();
		}
	    }
	    firstFileReader.close();
	    isData = false;
	    BufferedReader secondFileReader = new BufferedReader(new FileReader(secondArff));
	    while ((nextLine = secondFileReader.readLine()) != null) {
		if (isData) {
		    writer.append(nextLine);
		    writer.newLine();
		}
		if (nextLine.contains("@DATA")) {
		    isData = true;
		}
	    }
	    secondFileReader.close();
	    writer.close();
	    firstArff.delete();
	    secondArff.delete();
	}
}
